package org.jeecg.modules.gzgl01.controller;

import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.gzgl.controller.FxyGzgl04Controller;
import org.jeecg.modules.gzgl.entity.FxyGzgl01;
import org.jeecg.modules.gzgl.entity.FxyGzgl03;
import org.jeecg.modules.gzgl.entity.FxyGzgl04;
import org.jeecg.modules.gzgl.service.IFxyGzgl01Service;
import org.jeecg.modules.gzgl.service.IFxyGzgl04Service;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl02ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl03ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl05ServiceImpl;
import org.jeecg.modules.gzgl.service.impl.FxyGzgl06ServiceImpl;
import org.jeecg.modules.system.service.impl.SysDataLogServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
import java.util.List;

import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gzgl01.entity.FxyConcact01;
import org.jeecg.modules.gzgl01.entity.FxyConcact02;
import org.jeecg.modules.gzgl01.entity.FxyConcact;
import org.jeecg.modules.gzgl01.service.IFxyConcactService;
import org.jeecg.modules.gzgl01.service.IFxyConcact01Service;
import org.jeecg.modules.gzgl01.service.IFxyConcact02Service;


 /**
 * @Description: 合同
 * @Author: zhaodui
 * @Date:   2020-03-10
 * @Version: V1.0
 */
@RestController
@RequestMapping("/gzgl01/fxyConcact")
@Slf4j
public class FxyConcactController extends JeecgController<FxyConcact, IFxyConcactService> {

	@Autowired
	private IFxyConcactService fxyConcactService;

	@Autowired
	private IFxyConcact01Service fxyConcact01Service;

	@Autowired
	private IFxyConcact02Service fxyConcact02Service;

	 @Autowired
	 private IFxyGzgl01Service fxyGzgl01Service;
	 @Autowired
	 private SysDataLogServiceImpl sysDataLogService;
	 @Autowired
	 private FxyGzgl04Controller fxyGzgl04Controller;
	 @Autowired
	 private IFxyGzgl04Service fxyGzgl04Service;
	 @Autowired
	 private RedisUtil redisUtil;
	 @Autowired
	 private FxyGzgl02ServiceImpl fxyGzgl02Service;
	 @Autowired
	 private FxyGzgl03ServiceImpl fxyGzgl03Service;
	 @Autowired
	 private FxyGzgl05ServiceImpl fxyGzgl05Service;

	 @Autowired
	 private FxyGzgl06ServiceImpl fxyGzgl06Service;


	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询
	 * @param fxyConcact
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FxyConcact fxyConcact,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FxyConcact> queryWrapper = QueryGenerator.initQueryWrapper(fxyConcact, req.getParameterMap());
		Page<FxyConcact> page = new Page<FxyConcact>(pageNo, pageSize);
		IPage<FxyConcact> pageList = fxyConcactService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
     *   添加
     * @param fxyConcact
     * @return
     */
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FxyConcact fxyConcact) {
    	fxyConcactService.save(fxyConcact);
        String para = fxyConcact.getFxy01();
		QueryWrapper<FxyGzgl01> queryWrapper1 = new QueryWrapper<FxyGzgl01>();
		queryWrapper1.eq("query07", para);
		List<FxyGzgl01> list01 =  fxyGzgl01Service.list(queryWrapper1);
		if(list01==null||list01.size()==0){//新增数据
			FxyGzgl01 fxyGzgl01 = new FxyGzgl01();
			fxyGzgl01.setQuery01(fxyConcact.getFxy01());
			fxyGzgl01.setQuery02(fxyConcact.getFxy02());
			fxyGzgl01.setQuery03(fxyConcact.getFxy03());
			fxyGzgl01Service.save(fxyGzgl01);
		}

		QueryWrapper<FxyGzgl03> queryWrapper3 = new QueryWrapper<FxyGzgl03>();
		queryWrapper1.eq("query07", "test");
		List<FxyGzgl03> list03 =  fxyGzgl03Service.list(queryWrapper3);
		if(list03!=null||list03.size()>0){//新增合同主表
			FxyGzgl03 t = list03.get(0);
			FxyGzgl03 ts= new FxyGzgl03();
			BeanUtils.copyProperties(t,ts);
			ts.setId(null);
			ts.setQuery07(fxyConcact.getFxy01());
			ts.setQuery08(fxyConcact.getFxy02());
			ts.setQuery09(fxyConcact.getFxy03());
			ts.setQuery12(fxyConcact.getFxy22());
			ts.setQuery13(fxyConcact.getFxy23());
			fxyGzgl03Service.save(ts);
		}
		QueryWrapper<FxyGzgl04> queryWrapper4 = new QueryWrapper<FxyGzgl04>();
		queryWrapper1.eq("query07", "test");
		List<FxyGzgl04> list04 =  fxyGzgl04Service.list(queryWrapper4);
		if(list04!=null||list04.size()>0){//新增合同字段
			for(FxyGzgl04 t: list04 ){
				FxyGzgl04 ts= new FxyGzgl04();
 				BeanUtils.copyProperties(t,ts);
 				ts.setId(null);
				fxyGzgl04Service.save(ts);
			}

		}

        return Result.ok("添加成功！");
    }

    /**
     *  编辑
     * @param fxyConcact
     * @return
     */
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FxyConcact fxyConcact) {
        fxyConcactService.updateById(fxyConcact);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        fxyConcactService.delMain(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.fxyConcactService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FxyConcact fxyConcact) {
        return super.exportXls(request, fxyConcact, FxyConcact.class, "合同");
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FxyConcact.class);
    }
	/*---------------------------------主表处理-end-------------------------------------*/
	

    /*--------------------------------子表处理-签约人员信息-begin----------------------------------------------*/
	/**
	 * 查询子表信息 会传入主表ID
	 * @return
	 */
	@GetMapping(value = "/listFxyConcact01ByMainId")
    public Result<?> listFxyConcact01ByMainId(FxyConcact01 fxyConcact01,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<FxyConcact01> queryWrapper = QueryGenerator.initQueryWrapper(fxyConcact01, req.getParameterMap());
        Page<FxyConcact01> page = new Page<FxyConcact01>(pageNo, pageSize);
        IPage<FxyConcact01> pageList = fxyConcact01Service.page(page, queryWrapper);
        return Result.ok(pageList);
    }

	/**
	 * 添加
	 * @param fxyConcact01
	 * @return
	 */
	@PostMapping(value = "/addFxyConcact01")
	public Result<?> addFxyConcact01(@RequestBody FxyConcact01 fxyConcact01) {
		fxyConcact01Service.save(fxyConcact01);
		return Result.ok("添加成功！");
	}

    /**
	 * 编辑
	 * @param fxyConcact01
	 * @return
	 */
	@PutMapping(value = "/editFxyConcact01")
	public Result<?> editFxyConcact01(@RequestBody FxyConcact01 fxyConcact01) {
		fxyConcact01Service.updateById(fxyConcact01);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/deleteFxyConcact01")
	public Result<?> deleteFxyConcact01(@RequestParam(name="id",required=true) String id) {
		fxyConcact01Service.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatchFxyConcact01")
	public Result<?> deleteBatchFxyConcact01(@RequestParam(name="ids",required=true) String ids) {
		this.fxyConcact01Service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

    /*--------------------------------子表处理-签约人员信息-end----------------------------------------------*/

    /*--------------------------------子表处理-公证附件-begin----------------------------------------------*/
	/**
	 * 查询子表信息 会传入主表ID
	 * @return
	 */
	@GetMapping(value = "/listFxyConcact02ByMainId")
    public Result<?> listFxyConcact02ByMainId(FxyConcact02 fxyConcact02,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<FxyConcact02> queryWrapper = QueryGenerator.initQueryWrapper(fxyConcact02, req.getParameterMap());
        Page<FxyConcact02> page = new Page<FxyConcact02>(pageNo, pageSize);
        IPage<FxyConcact02> pageList = fxyConcact02Service.page(page, queryWrapper);
        return Result.ok(pageList);
    }

	/**
	 * 添加
	 * @param fxyConcact02
	 * @return
	 */
	@PostMapping(value = "/addFxyConcact02")
	public Result<?> addFxyConcact02(@RequestBody FxyConcact02 fxyConcact02) {
		fxyConcact02Service.save(fxyConcact02);
		return Result.ok("添加成功！");
	}

    /**
	 * 编辑
	 * @param fxyConcact02
	 * @return
	 */
	@PutMapping(value = "/editFxyConcact02")
	public Result<?> editFxyConcact02(@RequestBody FxyConcact02 fxyConcact02) {
		fxyConcact02Service.updateById(fxyConcact02);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/deleteFxyConcact02")
	public Result<?> deleteFxyConcact02(@RequestParam(name="id",required=true) String id) {
		fxyConcact02Service.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatchFxyConcact02")
	public Result<?> deleteBatchFxyConcact02(@RequestParam(name="ids",required=true) String ids) {
		this.fxyConcact02Service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

    /*--------------------------------子表处理-公证附件-end----------------------------------------------*/




}
